<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>第 3 章 R语言基础 | 生态统计学原理</title>
  <meta name="description" content="这是生态学统计基础的课程文件" />
  <meta name="generator" content="bookdown 0.40 and GitBook 2.6.7" />

  <meta property="og:title" content="第 3 章 R语言基础 | 生态统计学原理" />
  <meta property="og:type" content="book" />
  
  <meta property="og:description" content="这是生态学统计基础的课程文件" />
  <meta name="github-repo" content="rstudio/bookdown-demo" />

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="第 3 章 R语言基础 | 生态统计学原理" />
  
  <meta name="twitter:description" content="这是生态学统计基础的课程文件" />
  

<meta name="author" content="沈国春、李勤，华东师范大学生态与环境科学学院" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="如何开始.html"/>
<link rel="next" href="常见生态数据及问题.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />








<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>


<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<style type="text/css">
  
  div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">生态统计学原理</a></li>

<li class="divider"></li>
<li class="chapter" data-level="1" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i><b>1</b> 前言</a>
<ul>
<li class="chapter" data-level="1.1" data-path="index.html"><a href="index.html#为什么要学生态统计"><i class="fa fa-check"></i><b>1.1</b> 为什么要学生态统计</a></li>
<li class="chapter" data-level="1.2" data-path="index.html"><a href="index.html#为什么要学r语言"><i class="fa fa-check"></i><b>1.2</b> 为什么要学R语言</a></li>
<li class="chapter" data-level="1.3" data-path="index.html"><a href="index.html#如何使用和修改本书"><i class="fa fa-check"></i><b>1.3</b> 如何使用和修改本书</a></li>
<li class="chapter" data-level="1.4" data-path="index.html"><a href="index.html#课程助教"><i class="fa fa-check"></i><b>1.4</b> 课程助教</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="如何开始.html"><a href="如何开始.html"><i class="fa fa-check"></i><b>2</b> 如何开始</a>
<ul>
<li class="chapter" data-level="2.1" data-path="如何开始.html"><a href="如何开始.html#计算机的一些基本原理"><i class="fa fa-check"></i><b>2.1</b> 计算机的一些基本原理</a></li>
<li class="chapter" data-level="2.2" data-path="如何开始.html"><a href="如何开始.html#安装r程序"><i class="fa fa-check"></i><b>2.2</b> 安装R程序</a></li>
<li class="chapter" data-level="2.3" data-path="如何开始.html"><a href="如何开始.html#运行r程序"><i class="fa fa-check"></i><b>2.3</b> 运行R程序</a></li>
<li class="chapter" data-level="2.4" data-path="如何开始.html"><a href="如何开始.html#停止r程序"><i class="fa fa-check"></i><b>2.4</b> 停止R程序</a></li>
<li class="chapter" data-level="2.5" data-path="如何开始.html"><a href="如何开始.html#安装rstudio"><i class="fa fa-check"></i><b>2.5</b> 安装Rstudio</a></li>
<li class="chapter" data-level="2.6" data-path="如何开始.html"><a href="如何开始.html#获取帮助"><i class="fa fa-check"></i><b>2.6</b> 获取帮助</a>
<ul>
<li class="chapter" data-level="2.6.1" data-path="如何开始.html"><a href="如何开始.html#本地帮助文档"><i class="fa fa-check"></i><b>2.6.1</b> 本地帮助文档</a></li>
<li class="chapter" data-level="2.6.2" data-path="如何开始.html"><a href="如何开始.html#在线帮助"><i class="fa fa-check"></i><b>2.6.2</b> 在线帮助</a></li>
<li class="chapter" data-level="2.6.3" data-path="如何开始.html"><a href="如何开始.html#直接询问ai"><i class="fa fa-check"></i><b>2.6.3</b> 直接询问AI</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="如何开始.html"><a href="如何开始.html#r软件包"><i class="fa fa-check"></i><b>2.7</b> R软件包</a>
<ul>
<li class="chapter" data-level="2.7.1" data-path="如何开始.html"><a href="如何开始.html#使用r软件包"><i class="fa fa-check"></i><b>2.7.1</b> 使用R软件包</a></li>
<li class="chapter" data-level="2.7.2" data-path="如何开始.html"><a href="如何开始.html#r软件包内容"><i class="fa fa-check"></i><b>2.7.2</b> R软件包内容</a></li>
<li class="chapter" data-level="2.7.3" data-path="如何开始.html"><a href="如何开始.html#安装r软件包"><i class="fa fa-check"></i><b>2.7.3</b> 安装R软件包</a></li>
</ul></li>
<li class="chapter" data-level="2.8" data-path="如何开始.html"><a href="如何开始.html#安装其他功能"><i class="fa fa-check"></i><b>2.8</b> 安装其他功能</a>
<ul>
<li class="chapter" data-level="2.8.1" data-path="如何开始.html"><a href="如何开始.html#版本控制软件"><i class="fa fa-check"></i><b>2.8.1</b> 版本控制软件</a></li>
<li class="chapter" data-level="2.8.2" data-path="如何开始.html"><a href="如何开始.html#编写r软件包的辅助工具"><i class="fa fa-check"></i><b>2.8.2</b> 编写R软件包的辅助工具</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="如何开始.html"><a href="如何开始.html#获取本书资料"><i class="fa fa-check"></i><b>2.9</b> 获取本书资料</a>
<ul>
<li class="chapter" data-level="2.9.1" data-path="如何开始.html"><a href="如何开始.html#仅查看本书资料"><i class="fa fa-check"></i><b>2.9.1</b> 仅查看本书资料</a></li>
<li class="chapter" data-level="2.9.2" data-path="如何开始.html"><a href="如何开始.html#查看和修改本书资料"><i class="fa fa-check"></i><b>2.9.2</b> 查看和修改本书资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="r语言基础.html"><a href="r语言基础.html"><i class="fa fa-check"></i><b>3</b> R语言基础</a>
<ul>
<li class="chapter" data-level="3.1" data-path="r语言基础.html"><a href="r语言基础.html#重要的快捷键"><i class="fa fa-check"></i><b>3.1</b> 重要的快捷键</a></li>
<li class="chapter" data-level="3.2" data-path="r语言基础.html"><a href="r语言基础.html#基本数据类型"><i class="fa fa-check"></i><b>3.2</b> 基本数据类型</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="r语言基础.html"><a href="r语言基础.html#字符"><i class="fa fa-check"></i><b>3.2.1</b> 字符</a></li>
<li class="chapter" data-level="3.2.2" data-path="r语言基础.html"><a href="r语言基础.html#数字"><i class="fa fa-check"></i><b>3.2.2</b> 数字</a></li>
<li class="chapter" data-level="3.2.3" data-path="r语言基础.html"><a href="r语言基础.html#逻辑值"><i class="fa fa-check"></i><b>3.2.3</b> 逻辑值</a></li>
<li class="chapter" data-level="3.2.4" data-path="r语言基础.html"><a href="r语言基础.html#类型转换"><i class="fa fa-check"></i><b>3.2.4</b> 类型转换</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="r语言基础.html"><a href="r语言基础.html#数据运算"><i class="fa fa-check"></i><b>3.3</b> 数据运算</a>
<ul>
<li class="chapter" data-level="3.3.1" data-path="r语言基础.html"><a href="r语言基础.html#数字运算"><i class="fa fa-check"></i><b>3.3.1</b> 数字运算</a></li>
<li class="chapter" data-level="3.3.2" data-path="r语言基础.html"><a href="r语言基础.html#逻辑和关系运算"><i class="fa fa-check"></i><b>3.3.2</b> 逻辑和关系运算</a></li>
<li class="chapter" data-level="3.3.3" data-path="r语言基础.html"><a href="r语言基础.html#字符运算"><i class="fa fa-check"></i><b>3.3.3</b> 字符运算</a></li>
</ul></li>
<li class="chapter" data-level="3.4" data-path="r语言基础.html"><a href="r语言基础.html#常见运算函数"><i class="fa fa-check"></i><b>3.4</b> 常见运算函数</a></li>
<li class="chapter" data-level="3.5" data-path="r语言基础.html"><a href="r语言基础.html#变量"><i class="fa fa-check"></i><b>3.5</b> 变量</a>
<ul>
<li class="chapter" data-level="3.5.1" data-path="r语言基础.html"><a href="r语言基础.html#变量构造"><i class="fa fa-check"></i><b>3.5.1</b> 变量构造</a></li>
<li class="chapter" data-level="3.5.2" data-path="r语言基础.html"><a href="r语言基础.html#赋值"><i class="fa fa-check"></i><b>3.5.2</b> 赋值</a></li>
</ul></li>
<li class="chapter" data-level="3.6" data-path="r语言基础.html"><a href="r语言基础.html#逻辑控制"><i class="fa fa-check"></i><b>3.6</b> 逻辑控制</a>
<ul>
<li class="chapter" data-level="3.6.1" data-path="r语言基础.html"><a href="r语言基础.html#条件"><i class="fa fa-check"></i><b>3.6.1</b> 条件</a></li>
<li class="chapter" data-level="3.6.2" data-path="r语言基础.html"><a href="r语言基础.html#循环"><i class="fa fa-check"></i><b>3.6.2</b> 循环</a></li>
</ul></li>
<li class="chapter" data-level="3.7" data-path="r语言基础.html"><a href="r语言基础.html#容器"><i class="fa fa-check"></i><b>3.7</b> 容器</a>
<ul>
<li class="chapter" data-level="3.7.1" data-path="r语言基础.html"><a href="r语言基础.html#向量"><i class="fa fa-check"></i><b>3.7.1</b> 向量</a></li>
<li class="chapter" data-level="3.7.2" data-path="r语言基础.html"><a href="r语言基础.html#矩阵"><i class="fa fa-check"></i><b>3.7.2</b> 矩阵</a></li>
<li class="chapter" data-level="3.7.3" data-path="r语言基础.html"><a href="r语言基础.html#数据框"><i class="fa fa-check"></i><b>3.7.3</b> 数据框</a></li>
<li class="chapter" data-level="3.7.4" data-path="r语言基础.html"><a href="r语言基础.html#列表"><i class="fa fa-check"></i><b>3.7.4</b> 列表</a></li>
</ul></li>
<li class="chapter" data-level="3.8" data-path="r语言基础.html"><a href="r语言基础.html#函数"><i class="fa fa-check"></i><b>3.8</b> 函数</a>
<ul>
<li class="chapter" data-level="3.8.1" data-path="r语言基础.html"><a href="r语言基础.html#函数的判别"><i class="fa fa-check"></i><b>3.8.1</b> 函数的判别</a></li>
<li class="chapter" data-level="3.8.2" data-path="r语言基础.html"><a href="r语言基础.html#函数的定义"><i class="fa fa-check"></i><b>3.8.2</b> 函数的定义</a></li>
</ul></li>
<li class="chapter" data-level="3.9" data-path="r语言基础.html"><a href="r语言基础.html#作业"><i class="fa fa-check"></i><b>3.9</b> 作业</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="常见生态数据及问题.html"><a href="常见生态数据及问题.html"><i class="fa fa-check"></i><b>4</b> 常见生态数据及问题</a></li>
<li class="chapter" data-level="5" data-path="数理统计基础.html"><a href="数理统计基础.html"><i class="fa fa-check"></i><b>5</b> 数理统计基础</a>
<ul>
<li class="chapter" data-level="5.0.1" data-path="数理统计基础.html"><a href="数理统计基础.html#协方差"><i class="fa fa-check"></i><b>5.0.1</b> 协方差</a></li>
<li class="chapter" data-level="5.0.2" data-path="数理统计基础.html"><a href="数理统计基础.html#偏相关系数"><i class="fa fa-check"></i><b>5.0.2</b> 偏相关系数</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="数据探索性分析.html"><a href="数据探索性分析.html"><i class="fa fa-check"></i><b>6</b> 数据探索性分析</a>
<ul>
<li class="chapter" data-level="6.1" data-path="数据探索性分析.html"><a href="数据探索性分析.html#初步探索分析"><i class="fa fa-check"></i><b>6.1</b> 初步探索分析</a>
<ul>
<li class="chapter" data-level="6.1.1" data-path="数据探索性分析.html"><a href="数据探索性分析.html#异常值和缺失值"><i class="fa fa-check"></i><b>6.1.1</b> 异常值和缺失值</a></li>
<li class="chapter" data-level="6.1.2" data-path="数据探索性分析.html"><a href="数据探索性分析.html#理解数据"><i class="fa fa-check"></i><b>6.1.2</b> 理解数据</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="数据探索性分析.html"><a href="数据探索性分析.html#排序"><i class="fa fa-check"></i><b>6.2</b> 排序</a>
<ul>
<li class="chapter" data-level="6.2.1" data-path="数据探索性分析.html"><a href="数据探索性分析.html#排序的原理"><i class="fa fa-check"></i><b>6.2.1</b> 排序的原理</a></li>
<li class="chapter" data-level="6.2.2" data-path="数据探索性分析.html"><a href="数据探索性分析.html#排序基本原理"><i class="fa fa-check"></i><b>6.2.2</b> 排序基本原理</a></li>
<li class="chapter" data-level="6.2.3" data-path="数据探索性分析.html"><a href="数据探索性分析.html#主成分分析"><i class="fa fa-check"></i><b>6.2.3</b> 主成分分析</a></li>
<li class="chapter" data-level="6.2.4" data-path="数据探索性分析.html"><a href="数据探索性分析.html#对应分析"><i class="fa fa-check"></i><b>6.2.4</b> 对应分析</a></li>
<li class="chapter" data-level="6.2.5" data-path="数据探索性分析.html"><a href="数据探索性分析.html#常用的几种排序分析"><i class="fa fa-check"></i><b>6.2.5</b> 常用的几种排序分析</a></li>
</ul></li>
<li class="chapter" data-level="6.3" data-path="数据探索性分析.html"><a href="数据探索性分析.html#聚类"><i class="fa fa-check"></i><b>6.3</b> 聚类</a>
<ul>
<li class="chapter" data-level="6.3.1" data-path="数据探索性分析.html"><a href="数据探索性分析.html#层次聚类"><i class="fa fa-check"></i><b>6.3.1</b> 层次聚类</a></li>
<li class="chapter" data-level="6.3.2" data-path="数据探索性分析.html"><a href="数据探索性分析.html#非层次聚类"><i class="fa fa-check"></i><b>6.3.2</b> 非层次聚类</a></li>
<li class="chapter" data-level="6.3.3" data-path="数据探索性分析.html"><a href="数据探索性分析.html#多元回归树"><i class="fa fa-check"></i><b>6.3.3</b> 多元回归树</a></li>
<li class="chapter" data-level="6.3.4" data-path="数据探索性分析.html"><a href="数据探索性分析.html#顺序聚类"><i class="fa fa-check"></i><b>6.3.4</b> 顺序聚类</a></li>
</ul></li>
<li class="chapter" data-level="6.4" data-path="数据探索性分析.html"><a href="数据探索性分析.html#作业-1"><i class="fa fa-check"></i><b>6.4</b> 作业</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="实验设计.html"><a href="实验设计.html"><i class="fa fa-check"></i><b>7</b> 实验设计</a></li>
<li class="chapter" data-level="8" data-path="经典统计检验.html"><a href="经典统计检验.html"><i class="fa fa-check"></i><b>8</b> 经典统计检验</a></li>
<li class="chapter" data-level="9" data-path="回归分析.html"><a href="回归分析.html"><i class="fa fa-check"></i><b>9</b> 回归分析</a>
<ul>
<li class="chapter" data-level="9.1" data-path="回归分析.html"><a href="回归分析.html#一元回归"><i class="fa fa-check"></i><b>9.1</b> 一元回归</a>
<ul>
<li class="chapter" data-level="9.1.1" data-path="回归分析.html"><a href="回归分析.html#一元线性模型"><i class="fa fa-check"></i><b>9.1.1</b> 一元线性模型</a></li>
<li class="chapter" data-level="9.1.2" data-path="回归分析.html"><a href="回归分析.html#类型变量"><i class="fa fa-check"></i><b>9.1.2</b> 类型变量</a></li>
<li class="chapter" data-level="9.1.3" data-path="回归分析.html"><a href="回归分析.html#多项式回归"><i class="fa fa-check"></i><b>9.1.3</b> 多项式回归</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="回归分析.html"><a href="回归分析.html#多元回归"><i class="fa fa-check"></i><b>9.2</b> 多元回归</a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="回归分析.html"><a href="回归分析.html#变量筛选"><i class="fa fa-check"></i><b>9.2.1</b> 变量筛选</a></li>
<li class="chapter" data-level="9.2.2" data-path="回归分析.html"><a href="回归分析.html#因子之间的相对重要性"><i class="fa fa-check"></i><b>9.2.2</b> 因子之间的相对重要性</a></li>
<li class="chapter" data-level="9.2.3" data-path="回归分析.html"><a href="回归分析.html#模型诊断"><i class="fa fa-check"></i><b>9.2.3</b> 模型诊断</a></li>
<li class="chapter" data-level="9.2.4" data-path="回归分析.html"><a href="回归分析.html#包含类型变量的多元回归"><i class="fa fa-check"></i><b>9.2.4</b> 包含类型变量的多元回归</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="回归分析.html"><a href="回归分析.html#广义线性回归"><i class="fa fa-check"></i><b>9.3</b> 广义线性回归</a>
<ul>
<li class="chapter" data-level="9.3.1" data-path="回归分析.html"><a href="回归分析.html#目前的困境"><i class="fa fa-check"></i><b>9.3.1</b> 目前的困境</a></li>
<li class="chapter" data-level="9.3.2" data-path="回归分析.html"><a href="回归分析.html#理论扩展"><i class="fa fa-check"></i><b>9.3.2</b> 理论扩展</a></li>
<li class="chapter" data-level="9.3.3" data-path="回归分析.html"><a href="回归分析.html#binary数据例子"><i class="fa fa-check"></i><b>9.3.3</b> binary数据例子</a></li>
<li class="chapter" data-level="9.3.4" data-path="回归分析.html"><a href="回归分析.html#展示回归结果"><i class="fa fa-check"></i><b>9.3.4</b> 展示回归结果</a></li>
</ul></li>
<li class="chapter" data-level="9.4" data-path="回归分析.html"><a href="回归分析.html#混合效应模型"><i class="fa fa-check"></i><b>9.4</b> 混合效应模型</a></li>
<li class="chapter" data-level="9.5" data-path="回归分析.html"><a href="回归分析.html#结构方程"><i class="fa fa-check"></i><b>9.5</b> 结构方程</a></li>
<li class="chapter" data-level="9.6" data-path="回归分析.html"><a href="回归分析.html#作业-2"><i class="fa fa-check"></i><b>9.6</b> 作业</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="谱系分析.html"><a href="谱系分析.html"><i class="fa fa-check"></i><b>10</b> 谱系分析</a></li>
<li class="chapter" data-level="11" data-path="时间序列分析.html"><a href="时间序列分析.html"><i class="fa fa-check"></i><b>11</b> 时间序列分析</a></li>
<li class="chapter" data-level="12" data-path="空间分析.html"><a href="空间分析.html"><i class="fa fa-check"></i><b>12</b> 空间分析</a></li>
<li class="chapter" data-level="13" data-path="网络分析.html"><a href="网络分析.html"><i class="fa fa-check"></i><b>13</b> 网络分析</a>
<ul>
<li class="chapter" data-level="13.1" data-path="网络分析.html"><a href="网络分析.html#物种共现网络"><i class="fa fa-check"></i><b>13.1</b> 物种共现网络</a></li>
</ul></li>
<li class="chapter" data-level="14" data-path="宏分析.html"><a href="宏分析.html"><i class="fa fa-check"></i><b>14</b> 宏分析</a></li>
<li class="divider"></li>
<li><a href="https://faculty.ecnu.edu.cn/_s31/sgc/main.psp" target="blank">个人主页</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">生态统计学原理</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="r语言基础" class="section level1 hasAnchor" number="3">
<h1><span class="header-section-number">第 3 章</span> R语言基础<a href="r语言基础.html#r语言基础" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<p>利用R软件做分析的基本方式是在R中编写R代码。那么R代码的编写方式有几种：</p>
<ol style="list-style-type: decimal">
<li><p>双击R程序图标之后，打开Rgui图形界面。然后就可以直接在R console的&gt;后边编写R代码。</p></li>
<li><p>然后单击File-&gt;New script，就可以打开R代码的编辑器，撰写代码。同时，该编辑器的一个功能是可以用Ctrl+R快捷键，把光标所在行的代码发送到R console中。</p></li>
<li><p>用IDE，比如Rstudio。</p></li>
</ol>
<div id="重要的快捷键" class="section level2 hasAnchor" number="3.1">
<h2><span class="header-section-number">3.1</span> 重要的快捷键<a href="r语言基础.html#重要的快捷键" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>上下方向键： 可以调取历史命令</p>
<p>Esc键： 取消当前运算</p>
</div>
<div id="基本数据类型" class="section level2 hasAnchor" number="3.2">
<h2><span class="header-section-number">3.2</span> 基本数据类型<a href="r语言基础.html#基本数据类型" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>R语言中，最常用的三种数据类型是<strong>字符</strong>、<strong>数字</strong>和<strong>逻辑量</strong>，对应的英文分别是<strong>character</strong>, <strong>numeric</strong>和<strong>logical</strong>。下面我们一种种来介绍。</p>
<p>在具体介绍基本数据类型之前，我们先了解一下为什么要分数据类型。之前我们讲过一个重要原则，计算机时很笨的，因此，我们需要给他一些明确的规则，这样它才能正确的执行</p>
<div id="字符" class="section level3 hasAnchor" number="3.2.1">
<h3><span class="header-section-number">3.2.1</span> 字符<a href="r语言基础.html#字符" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>R里面的字符类型是用两个单引号或双引号包括起来内容。</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="r语言基础.html#cb17-1" tabindex="-1"></a><span class="st">&#39;4&#39;</span> <span class="co">#字符，character</span></span></code></pre></div>
<pre><code>## [1] &quot;4&quot;</code></pre>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="r语言基础.html#cb19-1" tabindex="-1"></a><span class="st">&quot;5&quot;</span></span></code></pre></div>
<pre><code>## [1] &quot;5&quot;</code></pre>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="r语言基础.html#cb21-1" tabindex="-1"></a><span class="fu">class</span>(<span class="st">&#39;50&#39;</span>) <span class="co">#class函数可以判断某个值的类型</span></span></code></pre></div>
<pre><code>## [1] &quot;character&quot;</code></pre>
<p>两个引号必须要相互对应。不能出现开始的引号是单引号，结束的引号为双引号。</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="r语言基础.html#cb23-1" tabindex="-1"></a><span class="st">&#39;hee&quot; #这个是错误的</span></span></code></pre></div>
<p>R其实有一些内置的字符</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="r语言基础.html#cb24-1" tabindex="-1"></a>letters <span class="co">#26个小写英文字母</span></span></code></pre></div>
<pre><code>##  [1] &quot;a&quot; &quot;b&quot; &quot;c&quot; &quot;d&quot; &quot;e&quot; &quot;f&quot; &quot;g&quot; &quot;h&quot; &quot;i&quot; &quot;j&quot; &quot;k&quot; &quot;l&quot; &quot;m&quot; &quot;n&quot; &quot;o&quot; &quot;p&quot; &quot;q&quot; &quot;r&quot; &quot;s&quot;
## [20] &quot;t&quot; &quot;u&quot; &quot;v&quot; &quot;w&quot; &quot;x&quot; &quot;y&quot; &quot;z&quot;</code></pre>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="r语言基础.html#cb26-1" tabindex="-1"></a>LETTERS <span class="co">#26个大写英文字母</span></span></code></pre></div>
<pre><code>##  [1] &quot;A&quot; &quot;B&quot; &quot;C&quot; &quot;D&quot; &quot;E&quot; &quot;F&quot; &quot;G&quot; &quot;H&quot; &quot;I&quot; &quot;J&quot; &quot;K&quot; &quot;L&quot; &quot;M&quot; &quot;N&quot; &quot;O&quot; &quot;P&quot; &quot;Q&quot; &quot;R&quot; &quot;S&quot;
## [20] &quot;T&quot; &quot;U&quot; &quot;V&quot; &quot;W&quot; &quot;X&quot; &quot;Y&quot; &quot;Z&quot;</code></pre>
</div>
<div id="数字" class="section level3 hasAnchor" number="3.2.2">
<h3><span class="header-section-number">3.2.2</span> 数字<a href="r语言基础.html#数字" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="r语言基础.html#cb28-1" tabindex="-1"></a><span class="dv">3</span> <span class="co">#整数</span></span></code></pre></div>
<pre><code>## [1] 3</code></pre>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="r语言基础.html#cb30-1" tabindex="-1"></a><span class="fl">3.1415</span> <span class="co">#实数</span></span></code></pre></div>
<pre><code>## [1] 3.1415</code></pre>
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="r语言基础.html#cb32-1" tabindex="-1"></a><span class="fl">1e+10</span> <span class="co">#科学计数法</span></span></code></pre></div>
<pre><code>## [1] 1e+10</code></pre>
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="r语言基础.html#cb34-1" tabindex="-1"></a><span class="fl">1e-10</span> </span></code></pre></div>
<pre><code>## [1] 1e-10</code></pre>
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="r语言基础.html#cb36-1" tabindex="-1"></a><span class="fu">class</span>(<span class="dv">3</span>)</span></code></pre></div>
<pre><code>## [1] &quot;numeric&quot;</code></pre>
<p>R内置常数</p>
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb38-1"><a href="r语言基础.html#cb38-1" tabindex="-1"></a>pi <span class="co">#圆周率</span></span></code></pre></div>
<pre><code>## [1] 3.141593</code></pre>
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb40-1"><a href="r语言基础.html#cb40-1" tabindex="-1"></a><span class="fu">exp</span>(<span class="dv">1</span>) <span class="co">#自然对数</span></span></code></pre></div>
<pre><code>## [1] 2.718282</code></pre>
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="r语言基础.html#cb42-1" tabindex="-1"></a><span class="cn">Inf</span> <span class="co">#正无穷</span></span></code></pre></div>
<pre><code>## [1] Inf</code></pre>
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb44-1"><a href="r语言基础.html#cb44-1" tabindex="-1"></a><span class="sc">-</span><span class="cn">Inf</span> <span class="co">#负无穷</span></span></code></pre></div>
<pre><code>## [1] -Inf</code></pre>
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb46-1"><a href="r语言基础.html#cb46-1" tabindex="-1"></a><span class="cn">NA</span> <span class="co">#not available or missing value</span></span></code></pre></div>
<pre><code>## [1] NA</code></pre>
<div class="sourceCode" id="cb48"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb48-1"><a href="r语言基础.html#cb48-1" tabindex="-1"></a><span class="cn">NaN</span> <span class="co">#not a number</span></span></code></pre></div>
<pre><code>## [1] NaN</code></pre>
</div>
<div id="逻辑值" class="section level3 hasAnchor" number="3.2.3">
<h3><span class="header-section-number">3.2.3</span> 逻辑值<a href="r语言基础.html#逻辑值" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb50"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb50-1"><a href="r语言基础.html#cb50-1" tabindex="-1"></a><span class="cn">TRUE</span> <span class="co">#True，真</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb52-1"><a href="r语言基础.html#cb52-1" tabindex="-1"></a>T</span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb54"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb54-1"><a href="r语言基础.html#cb54-1" tabindex="-1"></a><span class="cn">FALSE</span> <span class="co">#False, 假</span></span></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<div class="sourceCode" id="cb56"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb56-1"><a href="r语言基础.html#cb56-1" tabindex="-1"></a>F</span></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<div class="sourceCode" id="cb58"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb58-1"><a href="r语言基础.html#cb58-1" tabindex="-1"></a><span class="fu">class</span>(T)</span></code></pre></div>
<pre><code>## [1] &quot;logical&quot;</code></pre>
</div>
<div id="类型转换" class="section level3 hasAnchor" number="3.2.4">
<h3><span class="header-section-number">3.2.4</span> 类型转换<a href="r语言基础.html#类型转换" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>不同的数据类型之间可以相互转换。比如将逻辑性转化为数字型</p>
<div class="sourceCode" id="cb60"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb60-1"><a href="r语言基础.html#cb60-1" tabindex="-1"></a><span class="fu">as.numeric</span>(<span class="cn">TRUE</span>)</span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb62"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb62-1"><a href="r语言基础.html#cb62-1" tabindex="-1"></a><span class="fu">as.numeric</span>(<span class="cn">FALSE</span>)</span></code></pre></div>
<pre><code>## [1] 0</code></pre>
<p>反之也可以将数字型转化为逻辑性。</p>
<div class="sourceCode" id="cb64"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb64-1"><a href="r语言基础.html#cb64-1" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="dv">0</span>)</span></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<div class="sourceCode" id="cb66"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb66-1"><a href="r语言基础.html#cb66-1" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="dv">1</span>)</span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb68"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb68-1"><a href="r语言基础.html#cb68-1" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="dv">2</span>)</span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<p>除了0之外，其他数字转换为逻辑变量类型的值都为TRUE。</p>
<p>由于数据类型很多，因此有很多的数据类型准换函数，但基本的结构是：as.[数据类型]，方括号中的就是要转换为的数据类型。例如as.character等。当然，也存在is.[数据类型]来判断该值是什么数据类型。例如is.numeric(); is.logical(); is.character()等等。</p>
<p>当然，在R语言当中，除了上述三种常见的数据类型之外，还有许多其他的数据类型。有些是内置的，有些则是用户自定义的。为了简单起见，我们先不谈其他，用到时在解释也不迟。</p>
</div>
</div>
<div id="数据运算" class="section level2 hasAnchor" number="3.3">
<h2><span class="header-section-number">3.3</span> 数据运算<a href="r语言基础.html#数据运算" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="数字运算" class="section level3 hasAnchor" number="3.3.1">
<h3><span class="header-section-number">3.3.1</span> 数字运算<a href="r语言基础.html#数字运算" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>针对R中的数值，可以进行基本的算数预算</p>
<div class="sourceCode" id="cb70"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb70-1"><a href="r语言基础.html#cb70-1" tabindex="-1"></a><span class="dv">1</span><span class="sc">+</span><span class="dv">4</span><span class="sc">+</span><span class="dv">5</span></span></code></pre></div>
<pre><code>## [1] 10</code></pre>
<div class="sourceCode" id="cb72"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb72-1"><a href="r语言基础.html#cb72-1" tabindex="-1"></a><span class="dv">6</span><span class="sc">+</span><span class="dv">7</span></span></code></pre></div>
<pre><code>## [1] 13</code></pre>
<div class="sourceCode" id="cb74"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb74-1"><a href="r语言基础.html#cb74-1" tabindex="-1"></a><span class="dv">2-1</span></span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb76"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb76-1"><a href="r语言基础.html#cb76-1" tabindex="-1"></a><span class="dv">6</span><span class="sc">+</span><span class="dv">7</span>; <span class="dv">2-1</span></span></code></pre></div>
<pre><code>## [1] 13</code></pre>
<pre><code>## [1] 1</code></pre>
<p>乘除预算</p>
<div class="sourceCode" id="cb79"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb79-1"><a href="r语言基础.html#cb79-1" tabindex="-1"></a><span class="dv">4</span><span class="sc">*</span><span class="dv">6</span></span></code></pre></div>
<pre><code>## [1] 24</code></pre>
<div class="sourceCode" id="cb81"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb81-1"><a href="r语言基础.html#cb81-1" tabindex="-1"></a><span class="dv">1</span><span class="sc">/</span><span class="dv">3</span></span></code></pre></div>
<pre><code>## [1] 0.3333333</code></pre>
<div class="sourceCode" id="cb83"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb83-1"><a href="r语言基础.html#cb83-1" tabindex="-1"></a><span class="fu">round</span>(<span class="dv">1</span><span class="sc">/</span><span class="dv">3</span>,<span class="dv">3</span>) <span class="co">#round 就是一个R函数</span></span></code></pre></div>
<pre><code>## [1] 0.333</code></pre>
<p>取整取余</p>
<div class="sourceCode" id="cb85"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb85-1"><a href="r语言基础.html#cb85-1" tabindex="-1"></a><span class="co">#取余数</span></span>
<span id="cb85-2"><a href="r语言基础.html#cb85-2" tabindex="-1"></a><span class="dv">10</span><span class="sc">%%</span><span class="dv">3</span> </span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb87"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb87-1"><a href="r语言基础.html#cb87-1" tabindex="-1"></a><span class="dv">10</span><span class="sc">%/%</span><span class="dv">3</span> <span class="co">#取整</span></span></code></pre></div>
<pre><code>## [1] 3</code></pre>
<p>幂运算</p>
<div class="sourceCode" id="cb89"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb89-1"><a href="r语言基础.html#cb89-1" tabindex="-1"></a><span class="dv">2</span><span class="sc">^</span><span class="dv">3</span> </span></code></pre></div>
<pre><code>## [1] 8</code></pre>
<div class="sourceCode" id="cb91"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb91-1"><a href="r语言基础.html#cb91-1" tabindex="-1"></a><span class="dv">2</span><span class="sc">^</span>(<span class="dv">1</span><span class="sc">/</span><span class="dv">3</span>)</span></code></pre></div>
<pre><code>## [1] 1.259921</code></pre>
<div id="运算优先级" class="section level4 hasAnchor" number="3.3.1.1">
<h4><span class="header-section-number">3.3.1.1</span> 运算优先级<a href="r语言基础.html#运算优先级" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>R语言中，各种运算其实是有先后顺序，即运算优先级。默认的运算优先级为：^, * /, + -。如果要打破这种优先级，那么就需要用小括号，来告诉R哪几步是一起预算的。</p>
<div class="sourceCode" id="cb93"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb93-1"><a href="r语言基础.html#cb93-1" tabindex="-1"></a><span class="dv">3</span><span class="sc">+</span><span class="dv">1</span><span class="sc">^</span><span class="dv">2</span><span class="sc">*</span><span class="dv">2</span></span></code></pre></div>
<pre><code>## [1] 5</code></pre>
<div class="sourceCode" id="cb95"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb95-1"><a href="r语言基础.html#cb95-1" tabindex="-1"></a>(<span class="dv">3</span><span class="sc">+</span><span class="dv">1</span>)<span class="sc">^</span><span class="dv">2</span><span class="sc">*</span><span class="dv">2</span></span></code></pre></div>
<pre><code>## [1] 32</code></pre>
<div class="sourceCode" id="cb97"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb97-1"><a href="r语言基础.html#cb97-1" tabindex="-1"></a><span class="dv">3</span><span class="sc">+</span><span class="dv">1</span><span class="sc">^</span>(<span class="dv">2</span><span class="sc">*</span><span class="dv">2</span>)</span></code></pre></div>
<pre><code>## [1] 4</code></pre>
</div>
</div>
<div id="逻辑和关系运算" class="section level3 hasAnchor" number="3.3.2">
<h3><span class="header-section-number">3.3.2</span> 逻辑和关系运算<a href="r语言基础.html#逻辑和关系运算" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>除了上述算数运算之外，还可以进行逻辑运算</p>
<div class="sourceCode" id="cb99"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb99-1"><a href="r语言基础.html#cb99-1" tabindex="-1"></a><span class="cn">TRUE</span> <span class="sc">&amp;</span> <span class="cn">TRUE</span> <span class="co">#与</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb101"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb101-1"><a href="r语言基础.html#cb101-1" tabindex="-1"></a><span class="cn">TRUE</span> <span class="sc">|</span> <span class="cn">FALSE</span> <span class="co">#或</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb103"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb103-1"><a href="r语言基础.html#cb103-1" tabindex="-1"></a><span class="sc">!</span><span class="cn">TRUE</span> <span class="co">#反</span></span></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<p>对数字也可以进行关系预算，比如大小判断</p>
<div class="sourceCode" id="cb105"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb105-1"><a href="r语言基础.html#cb105-1" tabindex="-1"></a><span class="dv">5</span><span class="sc">&gt;</span><span class="dv">2</span>   <span class="co">#大于</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb107"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb107-1"><a href="r语言基础.html#cb107-1" tabindex="-1"></a><span class="dv">4</span><span class="sc">&gt;=</span><span class="dv">4</span>  <span class="co">#大于等于</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb109"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb109-1"><a href="r语言基础.html#cb109-1" tabindex="-1"></a><span class="dv">4</span><span class="sc">==</span><span class="dv">4</span>  <span class="co">#等于</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb111"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb111-1"><a href="r语言基础.html#cb111-1" tabindex="-1"></a><span class="dv">4</span><span class="sc">&lt;</span><span class="dv">6</span>   <span class="co">#小于</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb113"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb113-1"><a href="r语言基础.html#cb113-1" tabindex="-1"></a><span class="dv">4</span><span class="sc">&lt;=</span><span class="dv">3</span>  <span class="co">#小于等于</span></span></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<div class="sourceCode" id="cb115"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb115-1"><a href="r语言基础.html#cb115-1" tabindex="-1"></a><span class="dv">6</span><span class="sc">!=</span><span class="dv">0</span>  <span class="co">#不等于</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
</div>
<div id="字符运算" class="section level3 hasAnchor" number="3.3.3">
<h3><span class="header-section-number">3.3.3</span> 字符运算<a href="r语言基础.html#字符运算" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>R中有很多针对字符进行预算的操作，多以函数形式体现。例如</p>
<div class="sourceCode" id="cb117"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb117-1"><a href="r语言基础.html#cb117-1" tabindex="-1"></a><span class="fu">nchar</span>(<span class="st">&#39;abcd&#39;</span>) <span class="co">#数多少个字符</span></span></code></pre></div>
<pre><code>## [1] 4</code></pre>
<div class="sourceCode" id="cb119"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb119-1"><a href="r语言基础.html#cb119-1" tabindex="-1"></a><span class="fu">substr</span>(<span class="st">&quot;abcd&quot;</span>, <span class="dv">2</span>, <span class="dv">3</span>) <span class="co">#提取其中的部分</span></span></code></pre></div>
<pre><code>## [1] &quot;bc&quot;</code></pre>
<div class="sourceCode" id="cb121"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb121-1"><a href="r语言基础.html#cb121-1" tabindex="-1"></a><span class="fu">strsplit</span>(<span class="st">&#39;9qa49qw87eatqwae8ladj&#39;</span>,<span class="at">split =</span> <span class="st">&quot;a&quot;</span>) <span class="co">#依据某些字符分割字符</span></span></code></pre></div>
<pre><code>## [[1]]
## [1] &quot;9q&quot;      &quot;49qw87e&quot; &quot;tqw&quot;     &quot;e8l&quot;     &quot;dj&quot;</code></pre>
<div class="sourceCode" id="cb123"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb123-1"><a href="r语言基础.html#cb123-1" tabindex="-1"></a><span class="fu">paste</span>(<span class="st">&#39;a&#39;</span>,<span class="st">&#39;b&#39;</span>,<span class="st">&#39;c&#39;</span>,<span class="at">sep =</span> <span class="st">&quot;-&quot;</span>) <span class="co">#将多个字符合并</span></span></code></pre></div>
<pre><code>## [1] &quot;a-b-c&quot;</code></pre>
<p>如果你对上述命令的具体用法有任何疑问，可以采用?+具体命令名称的方式来查看其帮助文档。</p>
</div>
</div>
<div id="常见运算函数" class="section level2 hasAnchor" number="3.4">
<h2><span class="header-section-number">3.4</span> 常见运算函数<a href="r语言基础.html#常见运算函数" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>为了方便运算，R语言中内置了很多常用的运算函数。需要时只要调用相应的函数名称就行，而不需要重复造车。</p>
<p>以下是一些常用函数，如果需要知道具体用法，请用？调出其帮助文档查看。</p>
<div class="sourceCode" id="cb125"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb125-1"><a href="r语言基础.html#cb125-1" tabindex="-1"></a><span class="fu">exp</span>(<span class="dv">1</span>) <span class="co">#以自然对数为底的幂函数</span></span></code></pre></div>
<pre><code>## [1] 2.718282</code></pre>
<div class="sourceCode" id="cb127"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb127-1"><a href="r语言基础.html#cb127-1" tabindex="-1"></a><span class="dv">3</span><span class="sc">^</span>(<span class="dv">5</span>) </span></code></pre></div>
<pre><code>## [1] 243</code></pre>
<div class="sourceCode" id="cb129"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb129-1"><a href="r语言基础.html#cb129-1" tabindex="-1"></a><span class="dv">2</span><span class="sc">^</span>(<span class="dv">1</span><span class="sc">/</span><span class="dv">2</span>) <span class="co">#square root</span></span></code></pre></div>
<pre><code>## [1] 1.414214</code></pre>
<div class="sourceCode" id="cb131"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb131-1"><a href="r语言基础.html#cb131-1" tabindex="-1"></a><span class="fu">sqrt</span>(<span class="dv">2</span>)</span></code></pre></div>
<pre><code>## [1] 1.414214</code></pre>
<div class="sourceCode" id="cb133"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb133-1"><a href="r语言基础.html#cb133-1" tabindex="-1"></a><span class="fu">log</span>(<span class="dv">2</span>,<span class="dv">2</span>) <span class="co">#对数函数，第二个参数时base</span></span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb135"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb135-1"><a href="r语言基础.html#cb135-1" tabindex="-1"></a><span class="fu">log</span>(<span class="dv">1</span>,<span class="dv">879</span>) </span></code></pre></div>
<pre><code>## [1] 0</code></pre>
<div class="sourceCode" id="cb137"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb137-1"><a href="r语言基础.html#cb137-1" tabindex="-1"></a><span class="fu">log</span>(<span class="fu">exp</span>(<span class="dv">1</span>))</span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb139"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb139-1"><a href="r语言基础.html#cb139-1" tabindex="-1"></a><span class="fu">factorial</span>(<span class="dv">5</span>) <span class="co">#阶乘</span></span></code></pre></div>
<pre><code>## [1] 120</code></pre>
<div class="sourceCode" id="cb141"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb141-1"><a href="r语言基础.html#cb141-1" tabindex="-1"></a><span class="fu">sin</span>(pi<span class="sc">/</span><span class="dv">6</span>) <span class="co">#三角函数</span></span></code></pre></div>
<pre><code>## [1] 0.5</code></pre>
<div class="sourceCode" id="cb143"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb143-1"><a href="r语言基础.html#cb143-1" tabindex="-1"></a><span class="fu">tan</span>(pi<span class="sc">/</span><span class="dv">4</span>) </span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb145"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb145-1"><a href="r语言基础.html#cb145-1" tabindex="-1"></a><span class="fu">abs</span>(<span class="sc">-</span><span class="dv">5</span>) <span class="co">#绝对值</span></span></code></pre></div>
<pre><code>## [1] 5</code></pre>
<div class="sourceCode" id="cb147"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb147-1"><a href="r语言基础.html#cb147-1" tabindex="-1"></a><span class="fu">choose</span>(<span class="dv">100</span>,<span class="dv">30</span>) <span class="co">#组合函数</span></span></code></pre></div>
<pre><code>## [1] 2.937234e+25</code></pre>
<div class="sourceCode" id="cb149"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb149-1"><a href="r语言基础.html#cb149-1" tabindex="-1"></a><span class="fu">runif</span>(<span class="dv">10</span>) <span class="co">#产生随机数的函数</span></span></code></pre></div>
<pre><code>##  [1] 0.79379976 0.92912092 0.23911743 0.21060185 0.11283413 0.43969253
##  [7] 0.04591014 0.91822422 0.85601436 0.83677388</code></pre>
<div class="sourceCode" id="cb151"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb151-1"><a href="r语言基础.html#cb151-1" tabindex="-1"></a><span class="fu">all.equal</span>(<span class="fl">3.1</span>, <span class="fl">3.2</span>) <span class="co">#比较两个实数</span></span></code></pre></div>
<pre><code>## [1] &quot;Mean relative difference: 0.03225806&quot;</code></pre>
<div class="sourceCode" id="cb153"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb153-1"><a href="r语言基础.html#cb153-1" tabindex="-1"></a><span class="fu">is.infinite</span>(<span class="dv">10</span>) <span class="co">#判断是否是无穷数</span></span></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<div class="sourceCode" id="cb155"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb155-1"><a href="r语言基础.html#cb155-1" tabindex="-1"></a><span class="fu">is.na</span>(<span class="cn">NA</span>) <span class="co">#判断值是否为NA</span></span></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<p>保留小数位数</p>
<div class="sourceCode" id="cb157"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb157-1"><a href="r语言基础.html#cb157-1" tabindex="-1"></a><span class="fu">round</span>(pi,<span class="dv">2</span>)</span></code></pre></div>
<pre><code>## [1] 3.14</code></pre>
<div class="sourceCode" id="cb159"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb159-1"><a href="r语言基础.html#cb159-1" tabindex="-1"></a><span class="fu">ceiling</span>(pi)</span></code></pre></div>
<pre><code>## [1] 4</code></pre>
<div class="sourceCode" id="cb161"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb161-1"><a href="r语言基础.html#cb161-1" tabindex="-1"></a><span class="fu">floor</span>(pi)</span></code></pre></div>
<pre><code>## [1] 3</code></pre>
</div>
<div id="变量" class="section level2 hasAnchor" number="3.5">
<h2><span class="header-section-number">3.5</span> 变量<a href="r语言基础.html#变量" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="变量构造" class="section level3 hasAnchor" number="3.5.1">
<h3><span class="header-section-number">3.5.1</span> 变量构造<a href="r语言基础.html#变量构造" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>变量是一个值会变，但名字不变的量。例如</p>
<div class="sourceCode" id="cb163"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb163-1"><a href="r语言基础.html#cb163-1" tabindex="-1"></a>a<span class="ot">=</span><span class="dv">1</span> <span class="co">#a是一个变量，其中‘a’是名字，1是变量值，等号是赋值运算</span></span></code></pre></div>
<p>变量名通常由字母、数字和下划线组成，且不能以数字开头。此外，R语言中的变量名是大小写敏感的，且中间不能包含空格。</p>
<p>当仅输入变量名时，就可以查看变量值。</p>
<div class="sourceCode" id="cb164"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb164-1"><a href="r语言基础.html#cb164-1" tabindex="-1"></a>a</span></code></pre></div>
<pre><code>## [1] 1</code></pre>
</div>
<div id="赋值" class="section level3 hasAnchor" number="3.5.2">
<h3><span class="header-section-number">3.5.2</span> 赋值<a href="r语言基础.html#赋值" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>变量的值可以通过赋值运算来改变。正如上面的例子中的等号。但R语言中赋值运算不仅可以用等号，还可以用其他符号。例如</p>
<div class="sourceCode" id="cb166"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb166-1"><a href="r语言基础.html#cb166-1" tabindex="-1"></a>b<span class="ot">&lt;-</span><span class="dv">4</span> <span class="co">#&lt;-这也是一种赋值预算</span></span>
<span id="cb166-2"><a href="r语言基础.html#cb166-2" tabindex="-1"></a><span class="dv">5</span><span class="ot">-&gt;</span>c <span class="co">#-&gt;这也是一种赋值运算</span></span></code></pre></div>
<p>由于变量值可变，因此即便变量已经赋值了，还可以再次赋值，这样就会覆盖原来的值。</p>
<div class="sourceCode" id="cb167"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb167-1"><a href="r语言基础.html#cb167-1" tabindex="-1"></a>a<span class="ot">=</span><span class="dv">10</span></span></code></pre></div>
<p>由于R的数据类型本身有很多中，因此变量包含值，因此其也具有相应的数据类型的变量。</p>
<div class="sourceCode" id="cb168"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb168-1"><a href="r语言基础.html#cb168-1" tabindex="-1"></a>a<span class="ot">=</span><span class="dv">1</span>; <span class="fu">class</span>(a)</span></code></pre></div>
<pre><code>## [1] &quot;numeric&quot;</code></pre>
<div class="sourceCode" id="cb170"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb170-1"><a href="r语言基础.html#cb170-1" tabindex="-1"></a>b<span class="ot">=</span><span class="cn">TRUE</span>; <span class="fu">class</span>(b)</span></code></pre></div>
<pre><code>## [1] &quot;logical&quot;</code></pre>
<div class="sourceCode" id="cb172"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb172-1"><a href="r语言基础.html#cb172-1" tabindex="-1"></a>c<span class="ot">=</span><span class="st">&#39;a&#39;</span>; <span class="fu">class</span>(c)</span></code></pre></div>
<pre><code>## [1] &quot;character&quot;</code></pre>
</div>
</div>
<div id="逻辑控制" class="section level2 hasAnchor" number="3.6">
<h2><span class="header-section-number">3.6</span> 逻辑控制<a href="r语言基础.html#逻辑控制" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="条件" class="section level3 hasAnchor" number="3.6.1">
<h3><span class="header-section-number">3.6.1</span> 条件<a href="r语言基础.html#条件" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>单行条件语句ifelse</p>
<div class="sourceCode" id="cb174"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb174-1"><a href="r语言基础.html#cb174-1" tabindex="-1"></a><span class="fu">ifelse</span>(<span class="dv">5</span><span class="sc">&gt;</span><span class="dv">3</span>, <span class="st">&#39;yes&#39;</span>, <span class="st">&#39;no&#39;</span>)</span></code></pre></div>
<pre><code>## [1] &quot;yes&quot;</code></pre>
<p>多行的条件语句</p>
<div class="sourceCode" id="cb176"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb176-1"><a href="r语言基础.html#cb176-1" tabindex="-1"></a><span class="cf">if</span>(<span class="dv">4</span><span class="sc">&gt;</span><span class="dv">6</span>)</span>
<span id="cb176-2"><a href="r语言基础.html#cb176-2" tabindex="-1"></a>  <span class="fu">print</span>(<span class="st">&quot;Y&quot;</span>)</span>
<span id="cb176-3"><a href="r语言基础.html#cb176-3" tabindex="-1"></a><span class="fu">print</span>(<span class="st">&quot;N&quot;</span>)</span></code></pre></div>
<pre><code>## [1] &quot;N&quot;</code></pre>
<div class="sourceCode" id="cb178"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb178-1"><a href="r语言基础.html#cb178-1" tabindex="-1"></a><span class="cf">if</span>(<span class="dv">4</span><span class="sc">&gt;</span><span class="dv">6</span>){  <span class="co">#花括号表示当条件为真时的作用域</span></span>
<span id="cb178-2"><a href="r语言基础.html#cb178-2" tabindex="-1"></a>  <span class="fu">print</span>(<span class="st">&quot;Y&quot;</span>)</span>
<span id="cb178-3"><a href="r语言基础.html#cb178-3" tabindex="-1"></a>  <span class="fu">print</span>(<span class="st">&quot;N&quot;</span>)</span>
<span id="cb178-4"><a href="r语言基础.html#cb178-4" tabindex="-1"></a>}</span></code></pre></div>
<div class="sourceCode" id="cb179"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb179-1"><a href="r语言基础.html#cb179-1" tabindex="-1"></a><span class="cf">if</span>(<span class="dv">5</span><span class="sc">&gt;</span><span class="dv">3</span>){</span>
<span id="cb179-2"><a href="r语言基础.html#cb179-2" tabindex="-1"></a>  <span class="fu">print</span>(<span class="st">&quot;Yes&quot;</span>)</span>
<span id="cb179-3"><a href="r语言基础.html#cb179-3" tabindex="-1"></a>  <span class="dv">4</span><span class="sc">+</span><span class="dv">6</span></span>
<span id="cb179-4"><a href="r语言基础.html#cb179-4" tabindex="-1"></a>}<span class="cf">else</span>{</span>
<span id="cb179-5"><a href="r语言基础.html#cb179-5" tabindex="-1"></a>  <span class="fu">print</span>(<span class="st">&#39;No&#39;</span>)</span>
<span id="cb179-6"><a href="r语言基础.html#cb179-6" tabindex="-1"></a>}</span></code></pre></div>
<pre><code>## [1] &quot;Yes&quot;</code></pre>
<pre><code>## [1] 10</code></pre>
</div>
<div id="循环" class="section level3 hasAnchor" number="3.6.2">
<h3><span class="header-section-number">3.6.2</span> 循环<a href="r语言基础.html#循环" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb182"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb182-1"><a href="r语言基础.html#cb182-1" tabindex="-1"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>) <span class="co">#</span></span>
<span id="cb182-2"><a href="r语言基础.html#cb182-2" tabindex="-1"></a>  <span class="fu">print</span>(i)</span></code></pre></div>
<pre><code>## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10</code></pre>
<div class="sourceCode" id="cb184"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb184-1"><a href="r语言基础.html#cb184-1" tabindex="-1"></a><span class="fu">print</span>(i)</span></code></pre></div>
<pre><code>## [1] 10</code></pre>
<div class="sourceCode" id="cb186"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb186-1"><a href="r语言基础.html#cb186-1" tabindex="-1"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>){ <span class="co">#</span></span>
<span id="cb186-2"><a href="r语言基础.html#cb186-2" tabindex="-1"></a>  <span class="fu">print</span>(i)</span>
<span id="cb186-3"><a href="r语言基础.html#cb186-3" tabindex="-1"></a>  <span class="fu">print</span>(i)</span>
<span id="cb186-4"><a href="r语言基础.html#cb186-4" tabindex="-1"></a>}</span></code></pre></div>
<pre><code>## [1] 1
## [1] 1
## [1] 2
## [1] 2
## [1] 3
## [1] 3
## [1] 4
## [1] 4
## [1] 5
## [1] 5
## [1] 6
## [1] 6
## [1] 7
## [1] 7
## [1] 8
## [1] 8
## [1] 9
## [1] 9
## [1] 10
## [1] 10</code></pre>
<div class="sourceCode" id="cb188"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb188-1"><a href="r语言基础.html#cb188-1" tabindex="-1"></a>i<span class="ot">=</span><span class="dv">1</span></span>
<span id="cb188-2"><a href="r语言基础.html#cb188-2" tabindex="-1"></a><span class="cf">while</span>(i<span class="sc">&lt;</span><span class="dv">10</span>){</span>
<span id="cb188-3"><a href="r语言基础.html#cb188-3" tabindex="-1"></a>  <span class="fu">print</span>(i)</span>
<span id="cb188-4"><a href="r语言基础.html#cb188-4" tabindex="-1"></a>  i<span class="ot">=</span>i<span class="sc">+</span><span class="dv">1</span></span>
<span id="cb188-5"><a href="r语言基础.html#cb188-5" tabindex="-1"></a>}</span></code></pre></div>
<pre><code>## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9</code></pre>
</div>
</div>
<div id="容器" class="section level2 hasAnchor" number="3.7">
<h2><span class="header-section-number">3.7</span> 容器<a href="r语言基础.html#容器" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>目前我们依据讲过具有单个值的变量。这虽然有用，但还不够用。因为很多现实情况是，我们有很多个数据值。比如上千上万。在这种情况下，我们总不能构建一千个变量，用来保存一千个值吧？</p>
<p>因此，我们需要一个类似容器的变量，它可以保存多个值。这种变量我们形象的称为容器，因为它同时保存了多个值。</p>
<p>在R中，容器有很多类型，最常见的有向量(vector)、矩阵(matrix)、数据框(data.frame)、列表(list)等。下面我就一一介绍每种容器的定义、特征和操作方法。</p>
<div id="向量" class="section level3 hasAnchor" number="3.7.1">
<h3><span class="header-section-number">3.7.1</span> 向量<a href="r语言基础.html#向量" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div id="定义向量" class="section level4 hasAnchor" number="3.7.1.1">
<h4><span class="header-section-number">3.7.1.1</span> 定义向量<a href="r语言基础.html#定义向量" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>向量是R中最简单的一种容器。可以用c函数来定义。例如：</p>
<div class="sourceCode" id="cb190"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb190-1"><a href="r语言基础.html#cb190-1" tabindex="-1"></a>a<span class="ot">=</span><span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">3</span>)</span>
<span id="cb190-2"><a href="r语言基础.html#cb190-2" tabindex="-1"></a>a</span></code></pre></div>
<pre><code>## [1] 1 3</code></pre>
<p>上面的代码就用c（意为combine）构建了一个向量。其中a为向量名，而向量中的1或3为向量的元素。</p>
<p>除了这种构建方式之外，还有其他很多中方式构建。例如可以用:号来构建步长为1的连续整数向量：</p>
<div class="sourceCode" id="cb192"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb192-1"><a href="r语言基础.html#cb192-1" tabindex="-1"></a>b<span class="ot">=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">3</span></span>
<span id="cb192-2"><a href="r语言基础.html#cb192-2" tabindex="-1"></a>b</span></code></pre></div>
<pre><code>## [1] 1 2 3</code></pre>
<p>这就构建了名称为b，包含1，2，3三个元素的向量。</p>
<p>当然，冒号前后的数字大小可以调换，如果用下面命令构建向量：</p>
<div class="sourceCode" id="cb194"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb194-1"><a href="r语言基础.html#cb194-1" tabindex="-1"></a>c<span class="ot">=</span><span class="dv">3</span><span class="sc">:</span><span class="dv">1</span></span>
<span id="cb194-2"><a href="r语言基础.html#cb194-2" tabindex="-1"></a>c</span></code></pre></div>
<pre><code>## [1] 3 2 1</code></pre>
<p>则可以得到一个包含3，2，1三个元素的向量。注意，这个c向量与b向量的元素值的集合虽然都只包含1、2、3，但这些元素在向量中出现的顺序不一样。因此，向量中元素的顺序也是向量的一个特征。当元素顺序不同时，也就得到了一个新的向量。</p>
<p>如果要改变这些等差数列的步长，我们可以采用seq函数。</p>
<div class="sourceCode" id="cb196"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb196-1"><a href="r语言基础.html#cb196-1" tabindex="-1"></a>d<span class="ot">=</span><span class="fu">seq</span>(<span class="dv">1</span>,<span class="dv">10</span>,<span class="dv">2</span>)</span>
<span id="cb196-2"><a href="r语言基础.html#cb196-2" tabindex="-1"></a>d</span></code></pre></div>
<pre><code>## [1] 1 3 5 7 9</code></pre>
<p>这样，我们就构建了一个起始值为1，步长为2，最大值小于等于10的一个向量。</p>
</div>
<div id="向量的特征" class="section level4 hasAnchor" number="3.7.1.2">
<h4><span class="header-section-number">3.7.1.2</span> 向量的特征<a href="r语言基础.html#向量的特征" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>针对向量，我们可以用lenght函数计算向量的长度。</p>
<div class="sourceCode" id="cb198"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb198-1"><a href="r语言基础.html#cb198-1" tabindex="-1"></a><span class="fu">length</span>(a);<span class="fu">length</span>(b)</span></code></pre></div>
<pre><code>## [1] 2</code></pre>
<pre><code>## [1] 3</code></pre>
<p>向量内部可以放任何数据类型。</p>
<div class="sourceCode" id="cb201"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb201-1"><a href="r语言基础.html#cb201-1" tabindex="-1"></a>c<span class="ot">=</span><span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">3</span>)</span>
<span id="cb201-2"><a href="r语言基础.html#cb201-2" tabindex="-1"></a><span class="fu">class</span>(c)</span></code></pre></div>
<pre><code>## [1] &quot;numeric&quot;</code></pre>
<div class="sourceCode" id="cb203"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb203-1"><a href="r语言基础.html#cb203-1" tabindex="-1"></a>c<span class="ot">=</span><span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>)</span>
<span id="cb203-2"><a href="r语言基础.html#cb203-2" tabindex="-1"></a><span class="fu">class</span>(c)</span></code></pre></div>
<pre><code>## [1] &quot;logical&quot;</code></pre>
<div class="sourceCode" id="cb205"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb205-1"><a href="r语言基础.html#cb205-1" tabindex="-1"></a>c<span class="ot">=</span><span class="fu">c</span>(<span class="st">&#39;adg&#39;</span>,<span class="st">&#39;ad0&#39;</span>)</span>
<span id="cb205-2"><a href="r语言基础.html#cb205-2" tabindex="-1"></a><span class="fu">class</span>(c)</span></code></pre></div>
<pre><code>## [1] &quot;character&quot;</code></pre>
<p>但是针对一个向量，它内部的所有元素数据类型必须一致。</p>
<div class="sourceCode" id="cb207"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb207-1"><a href="r语言基础.html#cb207-1" tabindex="-1"></a>d<span class="ot">=</span><span class="fu">c</span>(<span class="dv">1</span>,<span class="st">&quot;a&quot;</span>)</span>
<span id="cb207-2"><a href="r语言基础.html#cb207-2" tabindex="-1"></a><span class="fu">class</span>(d[<span class="dv">1</span>])</span></code></pre></div>
<pre><code>## [1] &quot;character&quot;</code></pre>
<div class="sourceCode" id="cb209"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb209-1"><a href="r语言基础.html#cb209-1" tabindex="-1"></a>d<span class="ot">=</span><span class="fu">c</span>(<span class="dv">3</span>,<span class="cn">TRUE</span>,<span class="cn">FALSE</span>)</span>
<span id="cb209-2"><a href="r语言基础.html#cb209-2" tabindex="-1"></a>d</span></code></pre></div>
<pre><code>## [1] 3 1 0</code></pre>
<div class="sourceCode" id="cb211"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb211-1"><a href="r语言基础.html#cb211-1" tabindex="-1"></a>d<span class="ot">=</span><span class="fu">c</span>(<span class="cn">TRUE</span>,<span class="dv">3</span>)</span>
<span id="cb211-2"><a href="r语言基础.html#cb211-2" tabindex="-1"></a>d</span></code></pre></div>
<pre><code>## [1] 1 3</code></pre>
</div>
<div id="取向量的元素" class="section level4 hasAnchor" number="3.7.1.3">
<h4><span class="header-section-number">3.7.1.3</span> 取向量的元素<a href="r语言基础.html#取向量的元素" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>我们可以通过下标的方式来取向量的元素</p>
<div class="sourceCode" id="cb213"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb213-1"><a href="r语言基础.html#cb213-1" tabindex="-1"></a>a[<span class="dv">1</span>]</span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<p>其中，方括号中的数字代表下标，即第几个元素。但是如果下标为0或超过向量长度呢？会出现什么结果？请你们自己用R尝试一下回答。</p>
<p>我们可以给向量的每个元素一个名字，之后，就可以通过名字来取出对应的元素。</p>
<div class="sourceCode" id="cb215"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb215-1"><a href="r语言基础.html#cb215-1" tabindex="-1"></a><span class="fu">names</span>(a)<span class="ot">=</span><span class="fu">c</span>(<span class="st">&quot;zhang&quot;</span>,<span class="st">&quot;wang&quot;</span>)</span>
<span id="cb215-2"><a href="r语言基础.html#cb215-2" tabindex="-1"></a>a[<span class="st">&#39;zhang&#39;</span>]</span></code></pre></div>
<pre><code>## zhang 
##     1</code></pre>
</div>
<div id="修改向量长度" class="section level4 hasAnchor" number="3.7.1.4">
<h4><span class="header-section-number">3.7.1.4</span> 修改向量长度<a href="r语言基础.html#修改向量长度" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>当需要在向量内增加一个元素时，只需要通过下标的方式就可以完成。</p>
<div class="sourceCode" id="cb217"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb217-1"><a href="r语言基础.html#cb217-1" tabindex="-1"></a>a[<span class="dv">4</span>]<span class="ot">=</span><span class="dv">0</span></span></code></pre></div>
<p>如果我要将长度为4的向量a的第10个值赋为10，那么a将会变为什么？</p>
<div class="sourceCode" id="cb218"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb218-1"><a href="r语言基础.html#cb218-1" tabindex="-1"></a>a[<span class="dv">10</span>]<span class="ot">=</span><span class="dv">10</span></span>
<span id="cb218-2"><a href="r语言基础.html#cb218-2" tabindex="-1"></a>a</span></code></pre></div>
<pre><code>## zhang  wang                                                 
##     1     3    NA     0    NA    NA    NA    NA    NA    10</code></pre>
<p>如果要把向量中的某个元素删除，则仅需要通过带符号（-）的下标就可以实现。</p>
<div class="sourceCode" id="cb220"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb220-1"><a href="r语言基础.html#cb220-1" tabindex="-1"></a>a[<span class="sc">-</span><span class="dv">10</span>]</span></code></pre></div>
<pre><code>## zhang  wang                                           
##     1     3    NA     0    NA    NA    NA    NA    NA</code></pre>
<div class="sourceCode" id="cb222"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb222-1"><a href="r语言基础.html#cb222-1" tabindex="-1"></a>a[<span class="sc">-</span>(<span class="dv">5</span><span class="sc">:</span><span class="dv">9</span>)]</span></code></pre></div>
<pre><code>## zhang  wang                   
##     1     3    NA     0    10</code></pre>
</div>
<div id="向量元素的修改" class="section level4 hasAnchor" number="3.7.1.5">
<h4><span class="header-section-number">3.7.1.5</span> 向量元素的修改<a href="r语言基础.html#向量元素的修改" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>向量的元素值修改很简单，只要两个步骤。第一，用下标或名字的方法取到这个值，然后用赋值预算来修改这个元素对应的值。</p>
<div class="sourceCode" id="cb224"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb224-1"><a href="r语言基础.html#cb224-1" tabindex="-1"></a>a</span></code></pre></div>
<pre><code>## zhang  wang                                                 
##     1     3    NA     0    NA    NA    NA    NA    NA    10</code></pre>
</div>
</div>
<div id="矩阵" class="section level3 hasAnchor" number="3.7.2">
<h3><span class="header-section-number">3.7.2</span> 矩阵<a href="r语言基础.html#矩阵" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div id="矩阵定义" class="section level4 hasAnchor" number="3.7.2.1">
<h4><span class="header-section-number">3.7.2.1</span> 矩阵定义<a href="r语言基础.html#矩阵定义" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>矩阵与之前将的向量很相似，唯一的区别时向量是一维的，而矩阵是二维的。</p>
<div class="sourceCode" id="cb226"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb226-1"><a href="r语言基础.html#cb226-1" tabindex="-1"></a>a<span class="ot">=</span><span class="fu">matrix</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">30</span>,<span class="at">nrow=</span><span class="dv">5</span>,<span class="at">ncol=</span><span class="dv">6</span>)</span>
<span id="cb226-2"><a href="r语言基础.html#cb226-2" tabindex="-1"></a>a</span></code></pre></div>
<pre><code>##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    6   11   16   21   26
## [2,]    2    7   12   17   22   27
## [3,]    3    8   13   18   23   28
## [4,]    4    9   14   19   24   29
## [5,]    5   10   15   20   25   30</code></pre>
<p>矩阵的赋值默认是以列为优先，但如果要改变这种默认的规则，这需要设置其byrow的参数为TRUE。</p>
<div class="sourceCode" id="cb228"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb228-1"><a href="r语言基础.html#cb228-1" tabindex="-1"></a>b<span class="ot">=</span><span class="fu">matrix</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">30</span>,<span class="at">nrow=</span><span class="dv">5</span>,<span class="at">ncol=</span><span class="dv">6</span>, <span class="at">byrow=</span><span class="cn">TRUE</span>)</span>
<span id="cb228-2"><a href="r语言基础.html#cb228-2" tabindex="-1"></a>b</span></code></pre></div>
<pre><code>##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    2    3    4    5    6
## [2,]    7    8    9   10   11   12
## [3,]   13   14   15   16   17   18
## [4,]   19   20   21   22   23   24
## [5,]   25   26   27   28   29   30</code></pre>
</div>
<div id="矩阵的特征" class="section level4 hasAnchor" number="3.7.2.2">
<h4><span class="header-section-number">3.7.2.2</span> 矩阵的特征<a href="r语言基础.html#矩阵的特征" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>矩阵的维度</p>
<div class="sourceCode" id="cb230"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb230-1"><a href="r语言基础.html#cb230-1" tabindex="-1"></a><span class="fu">dim</span>(a)</span></code></pre></div>
<pre><code>## [1] 5 6</code></pre>
<div class="sourceCode" id="cb232"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb232-1"><a href="r语言基础.html#cb232-1" tabindex="-1"></a><span class="fu">nrow</span>(a); <span class="fu">ncol</span>(a)</span></code></pre></div>
<pre><code>## [1] 5</code></pre>
<pre><code>## [1] 6</code></pre>
</div>
<div id="给矩阵的行列名字" class="section level4 hasAnchor" number="3.7.2.3">
<h4><span class="header-section-number">3.7.2.3</span> 给矩阵的行列名字<a href="r语言基础.html#给矩阵的行列名字" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<div class="sourceCode" id="cb235"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb235-1"><a href="r语言基础.html#cb235-1" tabindex="-1"></a><span class="fu">colnames</span>(a)<span class="ot">=</span>letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">6</span>]</span>
<span id="cb235-2"><a href="r语言基础.html#cb235-2" tabindex="-1"></a>a</span></code></pre></div>
<pre><code>##      a  b  c  d  e  f
## [1,] 1  6 11 16 21 26
## [2,] 2  7 12 17 22 27
## [3,] 3  8 13 18 23 28
## [4,] 4  9 14 19 24 29
## [5,] 5 10 15 20 25 30</code></pre>
</div>
<div id="提取矩阵的元素" class="section level4 hasAnchor" number="3.7.2.4">
<h4><span class="header-section-number">3.7.2.4</span> 提取矩阵的元素<a href="r语言基础.html#提取矩阵的元素" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>用两个小标，中间带逗号。第一个小标表示行，第二个下标表示列。</p>
<div class="sourceCode" id="cb237"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb237-1"><a href="r语言基础.html#cb237-1" tabindex="-1"></a>a[<span class="dv">2</span>,<span class="dv">3</span>] <span class="co">#第二行，第三列的元素。</span></span></code></pre></div>
<pre><code>##  c 
## 12</code></pre>
<p>由于矩阵是二维的，我也可以取某一行或列。在这种情况下，只需要写一个下标就行。</p>
<div class="sourceCode" id="cb239"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb239-1"><a href="r语言基础.html#cb239-1" tabindex="-1"></a>a[<span class="dv">3</span>,] <span class="co">#第三行</span></span></code></pre></div>
<pre><code>##  a  b  c  d  e  f 
##  3  8 13 18 23 28</code></pre>
<div class="sourceCode" id="cb241"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb241-1"><a href="r语言基础.html#cb241-1" tabindex="-1"></a>a[,<span class="dv">2</span>] <span class="co">#第二列</span></span></code></pre></div>
<pre><code>## [1]  6  7  8  9 10</code></pre>
<p>如果想提取多行或多列，那么只需要给一个相应的向量下标就行。</p>
<p>按列或行名字提取</p>
<div class="sourceCode" id="cb243"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb243-1"><a href="r语言基础.html#cb243-1" tabindex="-1"></a><span class="fu">colnames</span>(a)<span class="ot">=</span>letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">6</span>]</span>
<span id="cb243-2"><a href="r语言基础.html#cb243-2" tabindex="-1"></a>a</span></code></pre></div>
<pre><code>##      a  b  c  d  e  f
## [1,] 1  6 11 16 21 26
## [2,] 2  7 12 17 22 27
## [3,] 3  8 13 18 23 28
## [4,] 4  9 14 19 24 29
## [5,] 5 10 15 20 25 30</code></pre>
<p>一旦有名字之后，就可以通过名字来访问列或行</p>
<div class="sourceCode" id="cb245"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb245-1"><a href="r语言基础.html#cb245-1" tabindex="-1"></a>a[,<span class="st">&#39;d&#39;</span>]</span></code></pre></div>
<pre><code>## [1] 16 17 18 19 20</code></pre>
</div>
<div id="修改矩阵元素" class="section level4 hasAnchor" number="3.7.2.5">
<h4><span class="header-section-number">3.7.2.5</span> 修改矩阵元素<a href="r语言基础.html#修改矩阵元素" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<div class="sourceCode" id="cb247"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb247-1"><a href="r语言基础.html#cb247-1" tabindex="-1"></a>a[<span class="dv">2</span>,<span class="dv">6</span>]<span class="ot">=</span><span class="dv">100</span></span>
<span id="cb247-2"><a href="r语言基础.html#cb247-2" tabindex="-1"></a>a</span></code></pre></div>
<pre><code>##      a  b  c  d  e   f
## [1,] 1  6 11 16 21  26
## [2,] 2  7 12 17 22 100
## [3,] 3  8 13 18 23  28
## [4,] 4  9 14 19 24  29
## [5,] 5 10 15 20 25  30</code></pre>
<div class="sourceCode" id="cb249"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb249-1"><a href="r语言基础.html#cb249-1" tabindex="-1"></a>b<span class="ot">=</span>a[<span class="sc">-</span><span class="dv">2</span>,]</span>
<span id="cb249-2"><a href="r语言基础.html#cb249-2" tabindex="-1"></a>b</span></code></pre></div>
<pre><code>##      a  b  c  d  e  f
## [1,] 1  6 11 16 21 26
## [2,] 3  8 13 18 23 28
## [3,] 4  9 14 19 24 29
## [4,] 5 10 15 20 25 30</code></pre>
</div>
</div>
<div id="数据框" class="section level3 hasAnchor" number="3.7.3">
<h3><span class="header-section-number">3.7.3</span> 数据框<a href="r语言基础.html#数据框" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>数据框是一种特殊的容器，它与向量类似，但也存在这几个不同点。第一，向量是一维的，而数据框是二维的。第二，向量内部的任何元素数据类型必须一样，而数据框可以包含不同的数据类型。不过，这种不同数据类型仅可以在不同列之间。在同一列内部，不同元素的数据类型必须一致。</p>
<div class="sourceCode" id="cb251"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb251-1"><a href="r语言基础.html#cb251-1" tabindex="-1"></a><span class="co">#创建数据框</span></span>
<span id="cb251-2"><a href="r语言基础.html#cb251-2" tabindex="-1"></a>da<span class="ot">=</span><span class="fu">data.frame</span>(<span class="at">a=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>,<span class="at">b=</span><span class="fu">c</span>(<span class="st">&quot;a&quot;</span>,<span class="st">&quot;b&quot;</span>,<span class="st">&quot;c&quot;</span>),<span class="at">c=</span><span class="fu">c</span>(F,T,F))</span></code></pre></div>
<p>通过data.frame函数，我们就可以定义一个类型为数据框的变量。其中每个参数是每一列的值。</p>
</div>
<div id="列表" class="section level3 hasAnchor" number="3.7.4">
<h3><span class="header-section-number">3.7.4</span> 列表<a href="r语言基础.html#列表" class="anchor-section" aria-label="Anchor link to header"></a></h3>
</div>
</div>
<div id="函数" class="section level2 hasAnchor" number="3.8">
<h2><span class="header-section-number">3.8</span> 函数<a href="r语言基础.html#函数" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="函数的判别" class="section level3 hasAnchor" number="3.8.1">
<h3><span class="header-section-number">3.8.1</span> 函数的判别<a href="r语言基础.html#函数的判别" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>R语言中已经存在着很多的函数，这让我们日常计算变得非常方便。</p>
<p>但我们怎么区分一个字符是函数名还是变量名？</p>
<div class="sourceCode" id="cb252"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb252-1"><a href="r语言基础.html#cb252-1" tabindex="-1"></a>sum</span></code></pre></div>
<pre><code>## function (..., na.rm = FALSE)  .Primitive(&quot;sum&quot;)</code></pre>
<div class="sourceCode" id="cb254"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb254-1"><a href="r语言基础.html#cb254-1" tabindex="-1"></a><span class="fu">sum</span>()</span></code></pre></div>
<pre><code>## [1] 0</code></pre>
<p>一般来讲，在小括号前面的字符串就是函数名。</p>
</div>
<div id="函数的定义" class="section level3 hasAnchor" number="3.8.2">
<h3><span class="header-section-number">3.8.2</span> 函数的定义<a href="r语言基础.html#函数的定义" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>函数可以简化编程，如果依据实现的功能，就只要直接调用已有的函数就行了。避免重复写同样的代码。</p>
<div class="sourceCode" id="cb256"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb256-1"><a href="r语言基础.html#cb256-1" tabindex="-1"></a>myfun<span class="ot">&lt;-</span><span class="cf">function</span>(<span class="at">par1=</span><span class="dv">1</span>, <span class="at">part1=</span><span class="dv">0</span>){ <span class="co">#花括号是定义了函数的范围</span></span>
<span id="cb256-2"><a href="r语言基础.html#cb256-2" tabindex="-1"></a>  re<span class="ot">=</span>part1<span class="sc">+</span><span class="dv">1</span></span>
<span id="cb256-3"><a href="r语言基础.html#cb256-3" tabindex="-1"></a>  <span class="fu">return</span>(re) <span class="co">#如果不写return，那么返回函数中最后一行的计算结果</span></span>
<span id="cb256-4"><a href="r语言基础.html#cb256-4" tabindex="-1"></a>}</span></code></pre></div>
<p>局域变量和全局变量</p>
<p>函数的调用</p>
</div>
</div>
<div id="作业" class="section level2 hasAnchor" number="3.9">
<h2><span class="header-section-number">3.9</span> 作业<a href="r语言基础.html#作业" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<ol style="list-style-type: decimal">
<li>请判断下面运算的结果，并在R console中验证你的结果</li>
</ol>
<div class="sourceCode" id="cb257"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb257-1"><a href="r语言基础.html#cb257-1" tabindex="-1"></a><span class="dv">100</span><span class="sc">+</span><span class="cn">Inf</span> </span>
<span id="cb257-2"><a href="r语言基础.html#cb257-2" tabindex="-1"></a><span class="dv">1</span><span class="sc">-</span><span class="cn">Inf</span></span>
<span id="cb257-3"><a href="r语言基础.html#cb257-3" tabindex="-1"></a><span class="dv">5</span><span class="sc">-</span><span class="st">&quot;1&quot;</span></span>
<span id="cb257-4"><a href="r语言基础.html#cb257-4" tabindex="-1"></a><span class="cn">TRUE</span> <span class="sc">&amp;</span> <span class="cn">NA</span></span>
<span id="cb257-5"><a href="r语言基础.html#cb257-5" tabindex="-1"></a>(<span class="dv">2</span><span class="sc">+</span><span class="cn">NA</span>)<span class="sc">/</span><span class="dv">2</span></span>
<span id="cb257-6"><a href="r语言基础.html#cb257-6" tabindex="-1"></a><span class="fu">exp</span>(<span class="sc">-</span><span class="cn">Inf</span>)</span>
<span id="cb257-7"><a href="r语言基础.html#cb257-7" tabindex="-1"></a><span class="dv">0</span><span class="sc">^</span><span class="cn">Inf</span></span>
<span id="cb257-8"><a href="r语言基础.html#cb257-8" tabindex="-1"></a><span class="dv">1</span><span class="sc">^</span><span class="cn">Inf</span></span>
<span id="cb257-9"><a href="r语言基础.html#cb257-9" tabindex="-1"></a><span class="dv">0</span><span class="sc">/</span><span class="dv">0</span></span>
<span id="cb257-10"><a href="r语言基础.html#cb257-10" tabindex="-1"></a><span class="cn">Inf</span><span class="sc">-</span><span class="cn">Inf</span></span>
<span id="cb257-11"><a href="r语言基础.html#cb257-11" tabindex="-1"></a><span class="cn">Inf</span><span class="sc">/</span><span class="cn">Inf</span></span>
<span id="cb257-12"><a href="r语言基础.html#cb257-12" tabindex="-1"></a>a<span class="ot">=</span><span class="cn">NA</span>; a<span class="sc">==</span><span class="cn">NA</span></span></code></pre></div>
<ol start="2" style="list-style-type: decimal">
<li>请对下面预算结果进行预测，并在R console中验证你的预测</li>
</ol>
<div class="sourceCode" id="cb258"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb258-1"><a href="r语言基础.html#cb258-1" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="dv">0</span>)</span>
<span id="cb258-2"><a href="r语言基础.html#cb258-2" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="sc">-</span><span class="dv">8</span>)</span>
<span id="cb258-3"><a href="r语言基础.html#cb258-3" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="st">&quot;&quot;</span>)</span>
<span id="cb258-4"><a href="r语言基础.html#cb258-4" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="st">&quot;a&quot;</span>)</span>
<span id="cb258-5"><a href="r语言基础.html#cb258-5" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="st">&#39;T&#39;</span>)</span>
<span id="cb258-6"><a href="r语言基础.html#cb258-6" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="st">&quot;True&quot;</span>)</span>
<span id="cb258-7"><a href="r语言基础.html#cb258-7" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="st">&quot;F&quot;</span>)</span>
<span id="cb258-8"><a href="r语言基础.html#cb258-8" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="st">&quot;FALSE&quot;</span>)</span>
<span id="cb258-9"><a href="r语言基础.html#cb258-9" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="sc">-</span><span class="cn">Inf</span>)</span>
<span id="cb258-10"><a href="r语言基础.html#cb258-10" tabindex="-1"></a><span class="fu">as.logical</span>(<span class="cn">NA</span>)</span>
<span id="cb258-11"><a href="r语言基础.html#cb258-11" tabindex="-1"></a></span>
<span id="cb258-12"><a href="r语言基础.html#cb258-12" tabindex="-1"></a><span class="fu">as.numeric</span>(<span class="cn">TRUE</span>)</span>
<span id="cb258-13"><a href="r语言基础.html#cb258-13" tabindex="-1"></a><span class="fu">as.numeric</span>(<span class="cn">FALSE</span>)</span>
<span id="cb258-14"><a href="r语言基础.html#cb258-14" tabindex="-1"></a><span class="fu">as.numeric</span>(<span class="st">&#39;4.55&#39;</span>)</span>
<span id="cb258-15"><a href="r语言基础.html#cb258-15" tabindex="-1"></a><span class="fu">as.numeric</span>(<span class="st">&#39;3e6&#39;</span>)</span>
<span id="cb258-16"><a href="r语言基础.html#cb258-16" tabindex="-1"></a><span class="fu">as.numeric</span>(<span class="cn">NA</span>)</span>
<span id="cb258-17"><a href="r语言基础.html#cb258-17" tabindex="-1"></a><span class="fu">as.numeric</span>(<span class="st">&#39;Inf&#39;</span>)</span>
<span id="cb258-18"><a href="r语言基础.html#cb258-18" tabindex="-1"></a></span>
<span id="cb258-19"><a href="r语言基础.html#cb258-19" tabindex="-1"></a><span class="fu">as.character</span>(<span class="dv">4</span>)</span>
<span id="cb258-20"><a href="r语言基础.html#cb258-20" tabindex="-1"></a><span class="fu">as.character</span>(T)</span>
<span id="cb258-21"><a href="r语言基础.html#cb258-21" tabindex="-1"></a><span class="fu">as.character</span>(<span class="st">&#39;asdg&#39;</span>)</span>
<span id="cb258-22"><a href="r语言基础.html#cb258-22" tabindex="-1"></a></span>
<span id="cb258-23"><a href="r语言基础.html#cb258-23" tabindex="-1"></a><span class="dv">1</span><span class="sc">+</span><span class="cn">FALSE</span></span>
<span id="cb258-24"><a href="r语言基础.html#cb258-24" tabindex="-1"></a><span class="dv">3</span><span class="sc">-</span><span class="cn">TRUE</span></span>
<span id="cb258-25"><a href="r语言基础.html#cb258-25" tabindex="-1"></a><span class="cn">TRUE</span><span class="sc">+</span><span class="st">&quot;a&quot;</span></span></code></pre></div>
<ol start="3" style="list-style-type: decimal">
<li>请帮忙预测并解释一下下面例子的结果，并说一说它对今后计算的意义</li>
</ol>
<div class="sourceCode" id="cb259"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb259-1"><a href="r语言基础.html#cb259-1" tabindex="-1"></a><span class="co">#例子1</span></span>
<span id="cb259-2"><a href="r语言基础.html#cb259-2" tabindex="-1"></a>x <span class="ot">=</span> <span class="fu">sqrt</span>(<span class="dv">2</span>)</span>
<span id="cb259-3"><a href="r语言基础.html#cb259-3" tabindex="-1"></a>x <span class="sc">*</span> x <span class="sc">==</span> <span class="dv">2</span></span>
<span id="cb259-4"><a href="r语言基础.html#cb259-4" tabindex="-1"></a></span>
<span id="cb259-5"><a href="r语言基础.html#cb259-5" tabindex="-1"></a><span class="co">#例子2</span></span>
<span id="cb259-6"><a href="r语言基础.html#cb259-6" tabindex="-1"></a>x <span class="ot">=</span> <span class="fl">0.3</span> <span class="sc">-</span> <span class="fl">0.2</span></span>
<span id="cb259-7"><a href="r语言基础.html#cb259-7" tabindex="-1"></a>y <span class="ot">=</span> <span class="fl">0.1</span></span>
<span id="cb259-8"><a href="r语言基础.html#cb259-8" tabindex="-1"></a>x <span class="sc">==</span> y</span></code></pre></div>
<ol start="4" style="list-style-type: decimal">
<li>帮我构建一个斐波那契序列，并打印出前46个值。其中斐波那契序列的定义如下：</li>
</ol>
<p><span class="math display">\[F_0=0, F_1=1, F_n=F_{n-1}+F_{n-2} (n \ge 2,n \in \mathbf{N})\]</span></p>
<ol start="5" style="list-style-type: decimal">
<li><p>在上一题的基础上，请给出多少位的时候，斐波那契序列首次出现正无穷。</p></li>
<li><p>请将asdjg12kjsdkgsdgs76876lllsdgas7810s9这串字符中的数字单个挑出，并求和。</p></li>
<li><p>请将上述字符中的连续数字挑出，并作为多位数，并将所有数字求和。</p></li>
<li><p>请读取生态岛的植物调查数据，并统计该调查资料中，总共有多少个个体？多少个物种？每个物种的多度是多少？同时，计算每个样方(subplot)内的上述值。最后，依据重要值的公式，定义一个函数，用于计算生态岛上每个物种的重要值。</p></li>
</ol>

</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="如何开始.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="常见生态数据及问题.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/USERNAME/REPO/edit/BRANCH/02-R_basic.Rmd",
"text": "编辑"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["_main.epub"],
"search": {
"engine": "fuse",
"options": null
},
"toc": {
"collapse": "subsection"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/latest.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
